package cn.dota2info.elk.controller;

import cn.dota2info.cleantorrent.CleanTorrent;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.io.File;
import java.io.FileOutputStream;


@RestController
public class UpLoadController {


    @RequestMapping(value = "/uploadTorrent", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Mono<String> uploadTorrent(@RequestPart("file") FilePart filePart) throws Exception {
        String basePath= System.getProperty("user.dir")+ File.separator;
        File file=new File(basePath+"tmp.torrent");
        filePart.transferTo(file);
        return Mono.just(CleanTorrent.getManageNet(basePath+"tmp.torrent"));
    }

    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath+fileName);
        out.write(file);
        out.flush();
        out.close();
    }
}
